{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# about Decorator\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 Introduction\n",
    "\n",
    "\n",
    "- 一种优雅地扩展函数功能的方法（联想到类的继承机制）：为了扩展函数的功能，不是改写她，而是在她的前后增加功能。\n",
    "- 使用了高阶函数(函数作为函数的参数)而形成的一个技巧。\n",
    "- 装饰器自身也能带参数，稍微复杂一点。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "last for 0.000679s\n",
      "last for 0.003828s\n"
     ]
    }
   ],
   "source": [
    "#%%writefile aboutDecorator/intro.py\n",
    "import time\n",
    "def times(fn):\n",
    "    def newFun(n):\n",
    "        start = time.time()\n",
    "        fn(n)\n",
    "        end = time.time()\n",
    "        print(\"last for {:2f}s\".format((end-start)))\n",
    "    return newFun          \n",
    "\n",
    "#标准语法\n",
    "@times   \n",
    "def compute1(n):\n",
    "    sum = 0\n",
    "    for i in range(0,n):\n",
    "        sum +=i\n",
    "    return sum\n",
    "\n",
    "\n",
    "#本质\n",
    "def compute2(n):\n",
    "    sum = 0\n",
    "    for i in range(0,n):\n",
    "        sum +=i**3\n",
    "    return sum\n",
    "compute2 = times(compute2)   \n",
    "    \n",
    "compute1(10000)    \n",
    "compute2(10000)    \n",
    "            "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2  decorator with parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "last for 4.974842ms\n",
      "last for 0.037808ms\n"
     ]
    }
   ],
   "source": [
    "#%%writefile aboutDecorator/para1.py\n",
    "import time\n",
    "\n",
    "def times(inter):\n",
    "    def real_dec(fn):\n",
    "        def newFun(n):\n",
    "            start = time.time()\n",
    "            fn(n)\n",
    "            end = time.time()\n",
    "            if inter ==1:\n",
    "                print(\"last for {:2f}s\".format((end-start)* inter))\n",
    "            else:\n",
    "                print(\"last for {:2f}ms\".format((end-start)* inter))\n",
    "        return newFun\n",
    "    return real_dec\n",
    "        \n",
    "@times(1000)\n",
    "def compute1(n):\n",
    "    sum = 0\n",
    "    for i in range(0,n):\n",
    "        sum +=i**3\n",
    "    return sum\n",
    "\n",
    "def compute2(n):\n",
    "    sum = 0\n",
    "    for i in range(0,n):\n",
    "        sum +=i**3\n",
    "    return sum\n",
    "compute2 = times(10)(compute2)\n",
    "\n",
    "\n",
    "compute1(10000)    \n",
    "compute2(10000)    \n",
    "            "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
